package com.alibaba.shopping_manager_api.controller;

import com.alibaba.shopping_common.pojo.Admin;
import com.alibaba.shopping_common.pojo.Permission;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.AdminService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName AdminController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/5 19:32
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @DubboReference
    private AdminService adminService;

    // 加密工具
    @Autowired
    private PasswordEncoder encoder;
    // @RequestBody：将请求体中的JSON、XML等格式的数据转化成对应的Java对象

    /**
     * 新增管理员
     * @param admin 管理员对象
     * @return 统一返回模型
     */
    @PostMapping("/add")
    public BaseResult addAdmin(@RequestBody Admin admin){
        String password = admin.getPassword();
        // 对密码进行加密
        String encode = encoder.encode(password);
        admin.setPassword(encode);
        adminService.addAdmin(admin);
        return BaseResult.OK();
    }

    // @RequestBody：将请求体中的JSON、XML等格式的数据转化成对应的Java对象

    /**
     * 更新管理员
     * @param admin 管理员对象
     * @return 统一返回模型
     */
    @PutMapping("/update")
    public BaseResult updateAdmin(@RequestBody Admin admin){
        String password = admin.getPassword();
        // 如果密码不为空
        if (StringUtils.hasText(password)){
            // 对密码进行加密
            String encode = encoder.encode(password);
            admin.setPassword(encode);
        }
        adminService.updateAdmin(admin);
        return BaseResult.OK();
    }

    /**
     * 删除管理员
     * @return 统一返回模型
     */
    @DeleteMapping("/delete")
    public BaseResult delAdmin(Long aid){
        adminService.delAdmin(aid);
        return BaseResult.OK();
    }

    /**
     * 根据id查询Admin的信息
     * @param aid 管理员id
     * @return 统一返回模型
     */
    @GetMapping("/findById")
    public BaseResult queryAdminById(Long aid){
        Admin admin = adminService.queryAdminById(aid);
        System.out.println(admin);
        return BaseResult.OK(admin);
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页大小
     * @return 页面模型
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/admin/search')") // 拥有这个权限才能访问
    public BaseResult<Page<Admin>> queryAdminByPage(int page,int size){
        Page<Admin> adminPage = adminService.queryAdminByPage(page, size);
        return BaseResult.OK(adminPage);
    }

    /**
     * 更新管理员权限信息
     * @param aid 管理员ID
     * @param rids 角色列表
     * @return 统一返回模型
     */
    @PutMapping("/updateRoleToAdmin")
    public BaseResult updateAdminRole(Long aid,Long [] rids){
        System.out.println("aid:"+aid);
        System.out.println("rids:"+ Arrays.toString(rids));
        adminService.updateAdminRole(aid,rids);
        return BaseResult.OK();
    }

    // todo：用于测试，上线要删除
    @GetMapping("/queryAdmin")
    public BaseResult<Admin> queryAdmin(String userName){
        Admin admin = adminService.findByAdminName(userName);
        return BaseResult.OK(admin);
    }

    // todo：用于测试，上线要删除
    @GetMapping("/queryAdminPermission")
    public BaseResult<List<Permission>> queryAdminPermission(String userName){
        List<Permission> allPermission = adminService.findAllPermission(userName);
        return BaseResult.OK(allPermission);
    }

    // 获取登录的管理员名称
    @GetMapping("/getUsername")
    public BaseResult<String> getUserName(){
        // 1. 获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        // 2. 获取认证对象
        Authentication authentication = context.getAuthentication();
        // 3. 获取用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String userName = userDetails.getUsername();
        return BaseResult.OK(userName);
    }
}




